package com.mine.frame.common.bus

import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.processors.PublishProcessor


/**
 * company 广东玛格家居有限公司
 * @author smoker
 * @Date 2021/11/12
 * Description:事件总线
 */
class RxBus private constructor() {
    private val mBus by lazy { PublishProcessor.create<RxBusBean>().toSerialized() }

    companion object {
        val instance = RxBusHolder.holder
    }

    object RxBusHolder {
        val holder = RxBus()
    }


    fun post(bus: RxBusBean) {
        mBus.onNext(bus)
    }

    fun toFlowable(): Flowable<RxBusBean> = mBus.ofType(RxBusBean::class.java)

    fun <T> toFlowable(tClass: Class<T>): Flowable<T> = mBus.ofType(tClass)

    fun hasSubscribers() = mBus.hasSubscribers()
}